package com.darwin.darwinIsometric.core
{
	import com.darwin.darwinBitmapEngine.core.ContainerNode;
	import com.darwin.darwinBitmapEngine.core.INode;
	import com.darwin.darwinBitmapEngine.core.Node;
	import com.darwin.darwinIsometric.utils.IsoUtils;
	import com.darwin.darwinUtils.BitmapDataTool;
	
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import spark.primitives.Rect;

	/**
	 * 世界格子类
	 * 2.5D中格子对象，可选择显示指定原形格子区域
	 * @author vanCopper
	 * 
	 */	
	public class IsoGrid extends ContainerNode
	{
		
		private var _xNum:int;
		private var _zNum:int;
		private var _size:int;
		public static const _OFF_SIZE:int = 6;
		
		private var _grid:Node;
		private var _gridBitmapData:BitmapData;
		private var _showAll:Boolean;
		
		private var _gridColor:uint;
		private var _gridThickness:Number
		/**
		 *  
		 * @param xNum x轴格子数
		 * @param zNum z轴格子数
		 * @param size 格子大小
		 * 
		 */		
		public function IsoGrid(xNum:int = 100,zNum:int = 100,size:int = 16,showAll:Boolean = false,gridColor:uint = 0xFF0000,thickness:Number = 2):void
		{
			_xNum = xNum;
			_zNum = zNum;
			_size = size;
			_showAll = showAll;
			_gridColor = gridColor;
			_gridThickness = thickness;
			init();
		}
		
		private var _offsetX:Number = 0;
		private function init():void
		{
			_gridBitmapData = getGridBitmapData(_gridColor,_gridThickness);
			_grid = new Node(_gridBitmapData);
//			_grid.x = _offsetX;
			_grid.x = -(_grid.width - _xNum*_size);
			addNode(_grid);
		}
		
		public function changeGridColor(gridColor:uint):void
		{
			
			var or:Number = _gridColor >> 16;
			var og:Number = _gridColor >> 8 & 0xFF;
			var ob:Number = _gridColor & 0xFF;

			var r:Number = gridColor >> 16;
			var g:Number = gridColor >> 8 & 0xFF;
			var b:Number = gridColor & 0xFF;
			
			var offr:Number = r - or;
			var offg:Number = g - og;
			var offb:Number = b - ob;
			
			_gridColor = gridColor;
			
			var colorTransform:ColorTransform = new ColorTransform(1,1,1,1,offr,offg,offb);
			_gridBitmapData.colorTransform(_gridBitmapData.rect,colorTransform);
		}
		
		private var _container:Sprite = new Sprite();
		private function getGridBitmapData(gridColor:uint , gridThickness:Number):BitmapData
		{
			/*var sp1:Sprite = new Sprite();
			sp1.graphics.beginFill(0xFFF000*Math.random());
			sp1.graphics.drawRect(0,0,100,100);
			sp1.graphics.endFill();
			var bp:BitmapData = new BitmapData(100,100);
			bp.draw(sp1);
			return bp;*/
			
			var _tx:int;// = _xNum + _OFF_SIZE;
			var _tz:int;// = _zNum + _OFF_SIZE;
			if(!_showAll)
			{
				_tx = _xNum + _OFF_SIZE;
				_tz = _zNum + _OFF_SIZE;
			}else
			{
				_tx = _xNum;
				_tz = _zNum;
			}
			
			for(var i:int = 0;i<_tx;i++)
			{
				for(var j:int = 0;j<_tz;j++)
				{
					var sp:Shape = new Shape();
					sp.graphics.clear();
					sp.graphics.lineStyle(gridThickness, gridColor, .5);
					sp.graphics.moveTo(-_size, _size * .5);
					sp.graphics.lineTo(0, 0);
					sp.graphics.lineTo(_size, _size * .5);
					sp.graphics.lineTo(0, _size);
					sp.graphics.lineTo(-_size, _size * .5);
					var isoPosition:Point3D = new Point3D(i*_size,0,j*_size);
					var screenPos:Point = IsoUtils.isoToScreen(isoPosition);
					sp.x = screenPos.x;
					sp.y = screenPos.y;
					_container.addChild(sp);
				}
			}
			
			if(!_showAll)
			{
				var containerMask:Sprite = createMask();
				containerMask.y = ((_zNum + _OFF_SIZE) * _size)/2;
				_container.mask = containerMask;
//				_container.addChild(containerMask);
			}
			
			var returnBitmapData:BitmapData = new BitmapData(_container.width,_container.height,true,0);
			var rect:Rectangle = _container.getRect(_container);
			var matrix:Matrix = new Matrix(1,0,0,1,-rect.left,-rect.top);
			_offsetX = rect.left;
			returnBitmapData.draw(_container,matrix);
			return returnBitmapData;
		}
		
		
		private function createMask():Sprite
		{
			var _maskBg:Sprite = new Sprite();
			var matrix:Matrix = new Matrix();
			
//			var mw:int = _container.width;
//			var mh:int = _container.height;
			var mw:int = ((_xNum + _OFF_SIZE) * _size * 2)*.9;
			var mh:int = ((_zNum + _OFF_SIZE) * _size)*.9;
			
			matrix.createGradientBox(mw,mh,0,-mw/2,-mh/2);
			_maskBg.graphics.beginGradientFill(GradientType.RADIAL, [0x0,0x0],[0.8,0],[150,220],matrix);
			_maskBg.graphics.drawEllipse(-mw/2,-mh/2,mw,mh);
			_maskBg.graphics.endFill();
			return _maskBg;
		}
		
		public static function getGrid(gw:Number,gh:Number,x:int,z:int):Node
		{
			var sprite:Sprite = new Sprite();
			
			var sp:Shape = new Shape();
			sp.graphics.lineStyle(2, 0x0000FF, .5);
			
			var p1:Point = IsoUtils.isoToScreen(new Point3D(0,0,gh));
			var p2:Point = IsoUtils.isoToScreen(new Point3D(gw,0,0));
			var p3:Point = IsoUtils.isoToScreen(new Point3D(gw,0,gh));
			
			sp.graphics.moveTo(p1.x,p1.y);
			sp.graphics.lineTo(0,0);
			sp.graphics.lineTo(p2.x,p2.y);
			sp.graphics.lineTo(p3.x,p3.y);
			sp.graphics.lineTo(p1.x,p1.y);
			
			var isoPosition:Point3D = new Point3D(gw,0,gh);
			
			var screenPos:Point = IsoUtils.isoToScreen(isoPosition);
			sprite.addChild(sp);
			
			var textFiled:TextField = new TextField();
			var textFormat:TextFormat = new TextFormat();
			textFormat.size = 16;
			textFiled.defaultTextFormat = textFormat
			textFiled.text = "" + x + ":" + z;
			textFiled.width = textFiled.textWidth + 20;
			textFiled.height = textFiled.textHeight + 20;
			sprite.addChild(textFiled);
			
			var rect:Rectangle = sprite.getRect(sprite);
			var bitmapData:BitmapData = BitmapDataTool.getBitmapData(sprite);
			var node:Node = new Node(bitmapData);
			node.x = rect.left;
			return node;
		}
		
		override public function render():void
		{
			_grid.render();
		}
		
		override public function get rect():Rectangle
		{
			return _grid.rect;	
		}
	}
}